package org.spiderflow.ocr.executor.shape;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.spiderflow.context.SpiderContext;
import org.spiderflow.executor.ShapeExecutor;
import org.spiderflow.model.Shape;
import org.spiderflow.model.SpiderNode;
import org.spiderflow.ocr.model.Ocr;
import org.springframework.stereotype.Component;

@Component
public class OcrExecutor implements ShapeExecutor{
	
	public static final String APP_ID = "appId";
	
	public static final String API_KEY = "apiKey";
	
	public static final String SECRET_KEY = "secretKey";
	
	public static final String OCR_CONTEXT_KEY = "$ocr_";
	
	@Override
	public String supportShape() {
		return "ocr";
	}
	
	@Override
	public Shape shape() {
		Shape shape = new Shape();
		shape.setImage("");
		shape.setLabel("OCR");
		shape.setName("ocr");
		shape.setTitle("OCR");
		return shape;
	}

	@Override
	public void execute(SpiderNode node, SpiderContext context, Map<String, Object> variables) {
		String appId = node.getStringJsonValue(APP_ID);
		String apiKey = node.getStringJsonValue(API_KEY);
		String secretKey = node.getStringJsonValue(SECRET_KEY);
		if (StringUtils.isBlank(appId)) {
			context.debug("appId不能为空！");
		} else if (StringUtils.isBlank(apiKey)) {
			context.debug("apiKey不能为空！");
		} else if (StringUtils.isBlank(secretKey)) {
			context.debug("secretKey不能为空！");
		} else {
			Ocr ocr = new Ocr(appId,apiKey,secretKey);
			context.put(OCR_CONTEXT_KEY + node.getNodeId(), ocr);
		}
	}

}
